<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>从文件指针中读入一行并解析 CSV 字段</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.fgetcsv" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">fgetcsv</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">fgetcsv</span> &mdash; <span class="dc-title">从文件指针中读入一行并解析 CSV 字段</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.fgetcsv-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>fgetcsv</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$handle</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code><span class="initializer"> = 0</span></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$delimiter</code><span class="initializer"> = &#039;,&#039;</span></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$enclosure</code><span class="initializer"> = &#039;&quot;&#039;</span></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$escape</code><span class="initializer"> = &#039;\\&#039;</span></span>
  ]]]] ) : <span class="type">array</span></div>

  <p class="para rdfs-comment">
   和 <span class="function"><a href="fgets.html" class="function">fgets()</a></span> 类似，只除了
   <span class="function"><strong>fgetcsv()</strong></span> 解析读入的行并找出 <acronym title="Comma Separated Value">CSV</acronym>
   格式的字段然后返回一个包含这些字段的数组。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.fgetcsv-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">handle</code></dt>

     <dd>

      <p class="para">
       一个由 <span class="function"><a href="fopen.html" class="function">fopen()</a></span>、<span class="function"><a href="popen.html" class="function">popen()</a></span> 或
       <span class="function"><a href="fsockopen.html" class="function">fsockopen()</a></span> 产生的有效文件指针。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">length</code></dt>

     <dd>

      <p class="para">
       必须大于 CVS 文件内最长的一行。在 PHP 5
       中该参数是可选的。如果忽略（在 PHP 5.0.4 以后的版本中设为
       0）该参数的话，那么长度就没有限制，不过可能会影响执行效率。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">delimiter</code></dt>

     <dd>

      <p class="para">
       设置字段分界符（只允许一个字符）。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">enclosure</code></dt>

     <dd>

      <p class="para">
       设置字段环绕符（只允许一个字符）。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">escape</code></dt>

     <dd>

      <p class="para">
       设置转义字符（只允许一个字符），默认是一个反斜杠。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.fgetcsv-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回包含读取字段的索引数组。
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">    
    CSV 文件中的空行将被返回为一个包含有单个 <span class="type"><a href="language.types.null.html" class="type null">null</a></span> 字段的数组，不会被当成错误。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">在读取在 Macintosh 电脑中或由其创建的文件时， 如果 PHP
不能正确的识别行结束符，启用运行时配置可选项 <a href="filesystem.configuration.html#ini.auto-detect-line-endings" class="link">auto_detect_line_endings</a>
也许可以解决此问题。</span></p></blockquote>
  <p class="para">
   如果提供了无效的文件指针，<span class="function"><strong>fgetcsv()</strong></span> 会返回 <strong><code>NULL</code></strong>。
   其他错误，包括碰到文件结束时返回 <strong><code>FALSE</code></strong>，。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.fgetcsv-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.3.0</td>
       <td>
        增加了 <code class="parameter">escape</code> 参数。
       </td>
      </tr>

      <tr>
       <td>4.3.5</td>
       <td>
        现在起 <span class="function"><strong>fgetcsv()</strong></span> 的操作是二进制安全的。
       </td>
      </tr>

      <tr>
       <td>4.3.0</td>
       <td>
        增加了 <code class="parameter">enclosure</code> 参数。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.fgetcsv-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2774">
    <p><strong>Example #1 读取并显示 CSV 文件的整个内容</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />if&nbsp;((</span><span style="color: #0000BB">$handle&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"test.csv"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"r"</span><span style="color: #007700">))&nbsp;!==&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;((</span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fgetcsv</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1000</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">","</span><span style="color: #007700">))&nbsp;!==&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$num&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;p&gt;&nbsp;</span><span style="color: #0000BB">$num</span><span style="color: #DD0000">&nbsp;fields&nbsp;in&nbsp;line&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #DD0000">:&nbsp;&lt;br&nbsp;/&gt;&lt;/p&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">++;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(</span><span style="color: #0000BB">$c</span><span style="color: #007700">=</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">$num</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$c</span><span style="color: #007700">++)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">[</span><span style="color: #0000BB">$c</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.fgetcsv-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    该函数对区域设置是敏感的。比如说 <var class="varname"><var class="varname">LANG</var></var> 设为
    <em>en_US.UTF-8</em> 的话，单字节编码的文件就会出现读取错误。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.fgetcsv-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="str_getcsv.html" class="function" rel="rdfs-seeAlso">str_getcsv()</a> - 解析 CSV 字符串为一个数组</span></li>
    <li class="member"><span class="function"><a href="explode.html" class="function" rel="rdfs-seeAlso">explode()</a> - 使用一个字符串分割另一个字符串</span></li>
    <li class="member"><span class="function"><a href="file.html" class="function" rel="rdfs-seeAlso">file()</a> - 把整个文件读入一个数组中</span></li>
    <li class="member"><span class="function"><a href="pack.html" class="function" rel="rdfs-seeAlso">pack()</a> - 将数据打包成二进制字符串</span></li>
    <li class="member"><span class="function"><a href="fputcsv.html" class="function" rel="rdfs-seeAlso">fputcsv()</a> - 将行格式化为 CSV 并写入文件指针</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>